package com.zhuanghdong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuanghdong.pojo.User;
import com.zhuanghdong.service.UserService;
import com.zhuanghdong.mapper.UserMapper;
import com.zhuanghdong.utils.JwtHelper;
import com.zhuanghdong.utils.MD5Util;
import com.zhuanghdong.utils.Result;
import com.zhuanghdong.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;

/**
* @author 庄泓东
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2024-05-01 15:04:33
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 登录板块
     * @param user
     * @return Result封装类
     */
    @Override
    public Result login(User user) {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User loginUser = userMapper.selectOne(queryWrapper);

        //账号错误
        if (loginUser == null) {
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }

        //判断密码
        if (!StringUtils.isEmpty(user.getUserPwd())
                && loginUser.getUserPwd().equals(MD5Util.encrypt(user.getUserPwd())))
        {
            //账号密码正确
            //根据用户唯一标识生成token
            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUid()));

            Map data = new HashMap();
            data.put("token",token);

            return Result.ok(data);
        }

        //密码错误
        return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);

    }

    /**
     * 获得用户信息板块
     * 根据token请求头来获取登录用户信息
     * @param token
     * @return
     */
    @Override
    public Result getUserInfo(String token) {

        //token不在有效期内，直接返回未登录
        if(jwtHelper.isExpiration(token)){
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }

        //获得对应的用户ID
        int userId=jwtHelper.getUserId(token).intValue();

        //根据用户ID查询数据
        User user=userMapper.selectById(userId);

        if(user!=null){
            //保护密码防止被盗取
            user.setUserPwd(null);
            Map data=new HashMap();
            data.put("loginUser",user);
            return Result.ok(data);
        }
        return Result.build(null,ResultCodeEnum.NOTLOGIN);
    }

    /**
     * 用户名检查板块
     * 检查用户名是否被使用板块
     * @param username
     * @return  result封装类
     */
    @Override
    public Result checkUserName(String username) {
        LambdaQueryWrapper<User> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);
        User user=userMapper.selectOne(queryWrapper);

        //用户已经存在
        if(user!=null){
            return Result.build(null,ResultCodeEnum.USERNAME_USED);
        }
        return Result.ok(null);
    }

    /**
     * 注册板块
     * @return result封装类
     */
    @Override
    public Result regist(User user) {
        LambdaQueryWrapper<User> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        Long count=userMapper.selectCount(queryWrapper);

        //用户数量大于1，失败
        if (count>0){
            return Result.build(null,ResultCodeEnum.USERNAME_USED);
        }

        //设置密码
        user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
        int rows=userMapper.insert(user);
        System.out.println("rows =" + rows);
        return Result.ok(null);
    }
}




